package edu.swe632.otm.resources;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResourceStore {
	
	private Map<Endpoints, List<Route>> routes = new HashMap<Endpoints, List<Route>>();
	
	public static ResourceStore SAVED = new ResourceStore("Saved");
	
	public static ResourceStore PUBLISHED = new ResourceStore("Published");
	
	private final String name;
	
	private ResourceStore(String name)
	{
		this.name = name;
	}
	
	public List<Route> getRoutesFor(String saddr, String daddr)
	{
		Endpoints endpoint = new Endpoints(saddr, daddr);
		return getRoutesFor(endpoint);
	}
	
	public List<Route> getRoutesFor(Endpoints endpoint)
	{
		List<Route> routeList = routes.get(endpoint);
		System.out.println(routeList);
		return routeList;
	}
	
	public Route getRouteForUser(String saddr, String daddr, String creatorUserName)
	{
		List<Route> endpointRoutes = getRoutesFor(saddr, daddr);
		if(endpointRoutes == null)
		{
			return null;
		}
		for(Route route : endpointRoutes)
		{
			if(route.getCreatorUserName().equals(creatorUserName))
			{
				return route;
			}
		}
		return null;
	}
	
	public List<Route> getAllComputerRoutes(String computerId)
	{
		List<Route> computerRoutes = new ArrayList<Route>();
		for (List<Route> routeList : routes.values()) {
			for (Route route : routeList) {
				if (route.getCreatorComputerId().equals(computerId)) {
					computerRoutes.add(route);
				}
			}
		}
		return computerRoutes;
	}
	
	public Route getRouteForComputer(String saddr, String daddr, String computerId)
	{
		List<Route> endpointRoutes = getRoutesFor(saddr, daddr);
		if(endpointRoutes == null)
		{
			return null;
		}
		for(Route route : endpointRoutes)
		{
			if(route.getCreatorComputerId().equals(computerId))
			{
				return route;
			}
		}
		return null;
	}
	
	public void removeRouteForUser(String saddr, String daddr, String userName)
	{
		Endpoints endpoint = new Endpoints(saddr, daddr);
		List<Route> endpointRoutes = routes.get(endpoint);
		if(endpointRoutes == null)
		{
			return;
		}
		Route toRemove = null;
		for(Route route : endpointRoutes)
		{
			String routeUser = route.getCreatorUserName();
			if(routeUser != null && routeUser.equals(userName))
			{
				toRemove = route;
			}
		}
		if(toRemove != null)
		{
			endpointRoutes.remove(toRemove);
		}
	}
	
	public void removeRouteForComputer(String saddr, String daddr, String computerId)
	{
		Endpoints endpoint = new Endpoints(saddr, daddr);
		List<Route> endpointRoutes = routes.get(endpoint);
		if(endpointRoutes == null)
		{
			return;
		}
		Route toRemove = null;
		for(Route route : endpointRoutes)
		{
			String routeComputer = route.getCreatorComputerId();
			if(routeComputer != null && routeComputer.equals(computerId))
			{
				toRemove = route;
			}
		}
		if(toRemove != null)
		{
			endpointRoutes.remove(toRemove);
		}
	}
	
	public void addRoute(Route route)
	{
		System.out.println("ADDING " + route + " to " + this);
		Endpoints endpoint = route.getEndpoint();
		List<Route> routeList = routes.get(endpoint);
		if(routeList == null)
		{
			routeList = new ArrayList<Route>();
			routes.put(endpoint, routeList);
		}
		routeList.add(route);
	}
	
	public Route getRoute(Endpoints endpoint, int number)
	{
		List<Route> routeList = getRoutesFor(endpoint);
		if(routeList != null && number < routeList.size())
		{
			return routeList.get(number);
		}
		return null;
	}
	
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder(name + ":" );
		for(Endpoints endpoint : routes.keySet())
		{
			builder.append(endpoint.toString());
		}
		return builder.toString();
	}
}
